package com.mlnx.stream.listener.iml;

import com.mlnx.analysis.pojo.dto.RealEcgAnalysResult;
import com.mlnx.common.utils.MyLog;
import com.mlnx.device.pojo.dto.DeviceStateRecord;
import com.mlnx.device.pojo.dto.EcgInfoMsg;
import com.mlnx.mptp.model.ecg.ECGData;
import com.mlnx.mptp.model.ecg.ECGDeviceInfo;
import com.mlnx.mptp.push.PushPacket;
import com.mlnx.mptp.push.body.PushDataType;
import com.mlnx.push.MlnxPushClientService;
import com.mlnx.push.TopicFactory;
import com.mlnx.stream.listener.EcgListener;
import com.mlnx.tp.base.model.DeviceType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

import cn.hutool.core.bean.BeanUtil;

/**
 * Created by amanda.shan on 2019/10/31.
 */
@Component
public class EcgPushListener implements EcgListener {

    private MyLog log = MyLog.getLog(getClass());

    @Autowired(required = false)
    private MlnxPushClientService mlnxPushService;

    @Autowired(required = false)
    private TopicFactory topicFactory;

    @Override
    public void receiveEcgInfo(EcgInfoMsg ecgInfoMsg) {

        // 推送设备信息
        ECGDeviceInfo ecgDeviceInfo = ecgInfoMsg.getEcgDeviceInfo();
        if (ecgDeviceInfo != null && ecgDeviceInfo.isDeviceInfoUpdate()) {
            String topic = topicFactory.getEcgDeviceInfoTopic(ecgInfoMsg.getPatientId());

            EcgInfoMsg msg = getNewEcgInfoMsg(ecgInfoMsg);

            pushEcgInfo(topic, msg);
        }


        // 分析结果
        RealEcgAnalysResult result = ecgInfoMsg.getRealEcgAnalysResult();
        if (result != null) {
            if (result.getHeart() != null || result.getHeartResult() != null || result.getPbNumb() !=
                    null) {

                String topic = topicFactory.getRealAnalyTopic(ecgInfoMsg.getPatientId());

                EcgInfoMsg msg = getNewEcgInfoMsg(ecgInfoMsg);

                RealEcgAnalysResult analysResult = new RealEcgAnalysResult();
                BeanUtil.copyProperties(result, analysResult, "ecgData", "filterEcgData");
                msg.setRealEcgAnalysResult(analysResult);

                pushEcgInfo(topic, msg);

                log.debug("推送心电分析结果:{}", msg);
            }
        }

        // 心率
        ECGData ecgData = ecgInfoMsg.getEcgData();
        if (ecgData != null) {
            Integer heart = null;
            if (ecgData.getEcgHeart() != null) {
                heart = ecgData.getEcgHeart();
            } else if (result != null && result.getHeart() != null) {
                heart = result.getHeart();
            }

            if (heart != null) {
                EcgInfoMsg msg = getNewEcgInfoMsg(ecgInfoMsg);

                ECGData data = new ECGData();
                data.setEcgHeart(heart);
                msg.setEcgData(data);

                String topic = topicFactory.getHeartTopic(ecgInfoMsg.getPatientId());
                pushEcgInfo(topic, msg);
                log.debug("推送心电心率:{}", heart);
            }
        }

    }

    public EcgInfoMsg getNewEcgInfoMsg(EcgInfoMsg ecgInfoMsg) {
        EcgInfoMsg msg = new EcgInfoMsg();
        msg.setEcgDeviceInfo(ecgInfoMsg.getEcgDeviceInfo());
        msg.setDeviceId(ecgInfoMsg.getDeviceId());
        msg.setPatientId(ecgInfoMsg.getPatientId());

        return msg;
    }

    private void pushEcgInfo(String topic, EcgInfoMsg msg) {
        PushPacket packet = new PushPacket();
        Map<PushDataType, Object> pushDataMap = new HashMap<>();
        pushDataMap.put(PushDataType.ECG_INFO, msg);
        packet.push(DeviceType.SERVER, topic, pushDataMap);

        mlnxPushService.push(topic, packet);
    }

    @Override
    public void castDeviceState(DeviceStateRecord deviceStateRecord) {

        String topic = topicFactory.getDeviceState(deviceStateRecord.getPatientId());

        PushPacket packet = new PushPacket();
        Map<PushDataType, Object> pushDataMap = new HashMap<>();
        pushDataMap.put(PushDataType.EXIGENCE_STATE, deviceStateRecord);
        packet.push(DeviceType.SERVER, topic, pushDataMap);

        mlnxPushService.push(topic, packet);
    }
}
